﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XXVIII.DAL.Factory;
using XXVIII.Entity;

namespace XXVIII.DAL.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            var formContext = DALFactory.CreateFormDAL();
            string tableName = "B_TbRole";
            var isExistTable = false;

            #region 表是否存在
            isExistTable = formContext.CheckTableExist(tableName);
            if (isExistTable)
            {
                Console.WriteLine(tableName + " 已存在");
            }
            #endregion

            #region 移除表结构
            if (isExistTable)
            {
                formContext.DropSignTable(tableName);

                //输出
                isExistTable = false;
                Console.WriteLine(tableName + " 已移除");
            }
            #endregion

            #region 创建表结构
            List<CreateCloumnEntity> createList = new List<CreateCloumnEntity>();
            CreateCloumnEntity id = new CreateCloumnEntity();
            id.ColumnName = "ID";
            id.ColumnType = "int";
            id.Length = 0;
            id.IsKey = true;
            id.IsIdentity = true;
            id.IdentityIncrement = 1;
            id.IdentitySeed = 1;
            id.IsNULL = false;
            id.Describe = "主键";
            createList.Add(id);

            CreateCloumnEntity roleName = new CreateCloumnEntity();
            roleName.ColumnName = "RoleName";
            roleName.ColumnType = "nvarchar";
            roleName.Length = 0;
            roleName.IsNULL = false;
            roleName.Describe = "角色名称";
            createList.Add(roleName);

            CreateCloumnEntity description = new CreateCloumnEntity();
            description.ColumnName = "Description";
            description.ColumnType = "nvarchar";
            description.Length = 512;
            description.IsNULL = true;
            description.Describe = "描述";
            createList.Add(description);

            CreateCloumnEntity creater = new CreateCloumnEntity();
            creater.ColumnName = "Creater";
            creater.ColumnType = "nvarchar";
            creater.Length = 512;
            creater.IsNULL = false;
            creater.Describe = "创建者";
            createList.Add(creater);


            CreateCloumnEntity createrTime = new CreateCloumnEntity();
            createrTime.ColumnName = "CreaterTime";
            createrTime.ColumnType = "datetime";
            createrTime.Length = 0;
            createrTime.IsNULL = false;
            createrTime.Describe = "创建时间";
            createList.Add(createrTime);


            CreateCloumnEntity updater = new CreateCloumnEntity();
            updater.ColumnName = "Updater";
            updater.ColumnType = "nvarchar";
            updater.Length = 512;
            updater.IsNULL = false;
            updater.Describe = "创建者";
            createList.Add(updater);


            CreateCloumnEntity updateTime = new CreateCloumnEntity();
            updateTime.ColumnName = "UpdateTime";
            updateTime.ColumnType = "datetime";
            updateTime.IsNULL = false;
            updateTime.Describe = "创建者";
            createList.Add(updateTime);

            if (!isExistTable)
            {
                formContext.CreateSignTable(tableName, createList);



                Console.WriteLine(tableName + " 已创建");
            }
            #endregion 

            #region 查询
            var allWhere = new List<TableAndOrLikeEntity>();
            TableAndOrLikeEntity where = new TableAndOrLikeEntity();
            where.ColumnName = "RoleName";
            where.Value = "limeng";
            where.AndOrLikeType = 1;
            allWhere.Add(where);
            var exist = formContext.Get(tableName, allWhere);
            Console.WriteLine(tableName + " 查询：" + exist.Rows.Count);
            #endregion

            #region 写入
            if (exist == null && exist.Rows.Count == 0)
            {
                Dictionary<string, string> keyValue = new Dictionary<string, string>();
                keyValue.Add("RoleName", "limeng");
                keyValue.Add("Description", "自动写入");
                keyValue.Add("Creater", "系统");
                keyValue.Add("CreaterTime", DateTime.Now.ToString());
                keyValue.Add("Updater", "系统");
                keyValue.Add("UpdateTime", DateTime.Now.ToString());
                var addRow = formContext.Add(tableName, keyValue);
            }
            #endregion

            #region 写入后查询
            exist = formContext.Get(tableName, allWhere);
            Console.WriteLine(tableName + " 写入后查询：" + exist.Rows.Count);
            #endregion

            #region 编辑
            if (exist != null && exist.Rows.Count > 0)
            {
                Dictionary<string, string> updateKeyValue = new Dictionary<string, string>();
                updateKeyValue.Add("Description", "自动编辑");
                updateKeyValue.Add("Creater", "马德华");
                updateKeyValue.Add("CreaterTime", DateTime.Now.ToString());
                updateKeyValue.Add("Updater", "刘德华");
                updateKeyValue.Add("UpdateTime", DateTime.Now.ToString());

                var updateAllWhere = new List<TableAndOrLikeEntity>();
                TableAndOrLikeEntity updateWhere = new TableAndOrLikeEntity();
                updateWhere.ColumnName = "RoleName";
                updateWhere.Value = "limeng";
                updateWhere.AndOrLikeType = 1;
                updateAllWhere.Add(where);
                var updateRow = formContext.Update(tableName, updateKeyValue, updateAllWhere);
                Console.WriteLine(tableName + " 已编辑: " + updateRow);
            }
            else
            {
                Console.WriteLine(tableName + " 无法编辑！");
            }
            #endregion

            #region 编辑后查询
            exist = formContext.Get(tableName, allWhere);
            Console.WriteLine(tableName + " 编辑后查询：" + exist.Rows.Count);
            #endregion

            #region 分页查询

            var pageWhereList = new List<TableAndOrLikeEntity>();
            TableAndOrLikeEntity pageWhere = new TableAndOrLikeEntity();
            pageWhere.ColumnName = "RoleName";
            pageWhere.Value = "limen";
            pageWhere.AndOrLikeType = 3;
            pageWhereList.Add(pageWhere);

            DBPageBase page = new DBPageBase();
            page.PageIndex = 1;
            page.PageSize = 20;
            formContext.GetByPage(tableName, pageWhereList, ref page);
            Console.WriteLine(tableName + " 分页查询：" + page.TotalRows);
            #endregion

            #region 移除
            if (exist != null && exist.Rows.Count > 0)
            {
                var delAllWhere = new List<TableAndOrLikeEntity>();
                TableAndOrLikeEntity delWhere = new TableAndOrLikeEntity();
                delWhere.ColumnName = "RoleName";
                delWhere.Value = "limeng";
                delWhere.AndOrLikeType = 1;
                delAllWhere.Add(delWhere);
                var delRow = formContext.Delete(tableName, delAllWhere);
                Console.WriteLine(tableName + " 已删除: " + delRow);
            }
            #endregion

            #region 移除后查询
            exist = formContext.Get(tableName, allWhere);
            Console.WriteLine(tableName + " 移除后查询：" + exist.Rows.Count);
            #endregion

            Console.ReadKey();
        }


        public static BlockBase WetlandPageRule(int pageIndex,int pageSize,List<TableDataCount> tdcList)
        {
            int startRow = pageSize * (pageIndex - 1);
            int endRow = startRow + pageSize;
            int blockStart = 0;
            int blockStartNum = 0;
            int blockEnd = 0;
            int blockEndNum = 0;

            int startAccumulation = 0;
            //起始点
            for (int i = 0; i < tdcList.Count; i++)
            {
                startAccumulation += tdcList[i].Count;
                if (startAccumulation - startRow >= 0)
                {
                    blockStartNum = tdcList[i].Count - (startAccumulation - startRow);
                    blockStart = i;
                    break;
                }
            }
            int endAccumulation = 0;
            //结束点
            for (int i = 0; i < tdcList.Count; i++)
            {
                endAccumulation += tdcList[i].Count;
                if (endAccumulation - endRow >= 0)
                {
                    blockEndNum = tdcList[i].Count - (endAccumulation - endRow);
                    blockEnd = i;
                    break;
                }
            }
            //最大页设置
            if (blockEnd == 0 && blockEndNum == 0)
            {
                blockEnd = tdcList.Count;
                blockEndNum = endAccumulation;
            }
            return new BlockBase() {
                BlockEndNum=blockEndNum,
                BlockEnd = blockEnd,
                BlockStartNum = blockStartNum,
                BlockStart = blockStart,
            };
        }
    }


    public class BlockBase
    {
        public int BlockStart  { get;set; }
        public int BlockStartNum { get; set; }
        public int BlockEnd { get; set; }
        public int BlockEndNum { get; set; }
    }

    public class TableDataCount
    {
        public int Count { get; set; }
        public string TableName { get; set; }
    }
}
